import { getZeroTime } from "../GlobalFunction";
import CardEntity from "../Models/Cards/CardEntity";
import CharacterEntity from "../Models/Characters/CharacterEntity";
import EquipEntity from "../Models/Equips/EquipEntity";
import GateEntity from "../Models/Gates/GateEntity";
import PetEntity from "../Models/Pets/PetEntity";
import ShopEntity from "../Models/Shops/ShopEntity";
import SigninEntity from "../Models/Signins/SigninEntity";
import SkillEntity from "../Models/Skills/SkillEntity";
import TaskEntity from "../Models/Tasks/TaskEntity";

export default class PlayerSaveData {
    public isFirstSetName: boolean = false;
    public playerName: string = "未命名";
    public isPlayBgm: boolean = true;
    public isPlayeAudioEffect: boolean = true;
    public playerHeadId: number = 1001;
    public gold: number;
    public diamond: number;
    public isPlayMovie: boolean = false;
    public heroList: CharacterEntity[];
    public gateEntity: GateEntity;
    /**已通关关卡ID列表 */
    public gatePassList: number[] = [];
    public skillList: SkillEntity[] = [];
    public equipWearList: number[] = [0, 0, 0, 0, 0];
    public equipBagList: EquipEntity[] = [];
    public petWearList: number[] = [-1, -1, -1, -1, -1];
    public petBagList: PetEntity[] = [];
    public shopList: ShopEntity[] = [];
    public shopResetTime: number = 0;
    public currentMapId: number = 0;
    public taskList: TaskEntity[] = [];
    public isSignin: boolean = false;
    public signinTime: number = 0;
    public signinDay: number = 0;
    public signinArr: SigninEntity[] = [];
    public monthCard: CardEntity = null;
    public perpetualCard: CardEntity = null;
    public hitAttack: number = 10;
    public guideId: number = 1;
    public addSpeedMode: number = 0;
    public isFirstChangeName: boolean = true;
    public fubenCount: number = 5;
    public fubenCountTime: number = 0;
    public giftArr: boolean[] = [];


    /**本地无数据时初始化 */
    initUserData() {
        this.guideId = 1;
        this.gold = 100;
        this.diamond = 10000;
        this.addSpeedMode = 0;
        this.isFirstChangeName = true;
        this.fubenCount = 5;
        this.fubenCountTime = getZeroTime();
        this.heroList = [new CharacterEntity({ id: 1001, lv: 1 }),
        new CharacterEntity({ id: 2001, lv: 1 }),
        new CharacterEntity({ id: 2002, lv: 1 }),
        new CharacterEntity({ id: 3001, lv: 1 })];
        this.gateEntity = new GateEntity();
        this.gateEntity.init({ currentGateId: 1 });
        this.gatePassList = [];
        this.skillList = [];
        this.equipWearList = [0, 0, 0, 0, 0];
        if (CC_EDITOR) {
            this.equipBagList = [new EquipEntity({ id: 10001, level: 1, isWear: false, exp: 0 }), new EquipEntity({ id: 10002, level: 1, isWear: false, exp: 0 }),
            new EquipEntity({ id: 10003, level: 1, isWear: false, exp: 0 }), new EquipEntity({ id: 10004, level: 1, isWear: false, exp: 0 }),
            new EquipEntity({ id: 10005, level: 1, isWear: false, exp: 0 })];
        } else {
            this.equipBagList = [];
        }
        this.petBagList = [];
        this.petWearList = [-1, -1, -1, -1, -1];
        this.shopList = [];
        this.shopResetTime = getZeroTime();
        this.currentMapId = 1;
        this.taskList = [];
        this.isSignin = false;
        this.signinTime = getZeroTime();
        this.signinDay = 0;
        for (let i = 0; i < 7; i++) {
            var signinEy = new SigninEntity();
            signinEy.day = i + 1;
            signinEy.isReceive = false;
            this.signinArr[i] = signinEy;
        }
        this.giftArr = [false, false, false, false];
    }
    /**
     * 继续游戏时调用
     * @param userdata 玩家数据
     */
    loadData(userdata: any) {
        this.guideId = userdata.guideId != null ? userdata.guideId : 1;
        this.isFirstSetName = userdata.isFirstSetName != null ? userdata.isFirstSetName : false;
        this.playerName = userdata.playerName != null ? userdata.playerName : "未命名";
        this.isPlayBgm = userdata.isPlayBgm != null ? userdata.isPlayBgm : true;
        this.isPlayeAudioEffect = userdata.isPlayeAudioEffect != null ? userdata.isPlayeAudioEffect : true;
        this.playerHeadId = userdata.playerHeadId != null ? userdata.playerHeadId : 1001;
        this.isPlayMovie = userdata.isPlayMovie != null ? userdata.isPlayMovie : false;
        this.gold = userdata.gold;
        this.diamond = userdata.diamond;
        this.addSpeedMode = userdata.addSpeedMode != null ? userdata.addSpeedMode : 0;
        this.isFirstChangeName = userdata.isFirstChangeName != null ? userdata.isFirstChangeName : true;
        this.fubenCount = userdata.fubenCount != null ? userdata.fubenCount : 5;
        this.fubenCountTime = userdata.fubenCountTime != null ? userdata.fubenCountTime : getZeroTime();
        var arr: any[] = userdata.heroList;
        this.heroList = [];
        for (let i = 0; i < arr.length; i++) {
            this.heroList.push(new CharacterEntity(arr[i]));
        }
        this.gateEntity = new GateEntity();
        this.gateEntity.init(userdata.gateEntity);
        this.gatePassList = userdata.gatePassList != null ? userdata.gatePassList : [];

        arr = userdata.skillList != null ? userdata.skillList : [];
        for (const iterator of arr) {
            var ey = new SkillEntity();
            ey.init(iterator);
            this.skillList.push(ey);
        }
        arr = userdata.equipBagList != null ? userdata.equipBagList : [];
        for (const iterator of arr) {
            this.equipBagList.push(new EquipEntity(iterator));
        }
        this.equipWearList = userdata.equipWearList != null ? userdata.equipWearList : [0, 0, 0, 0, 0];

        arr = userdata.petBagList != null ? userdata.petBagList : [];
        for (const iterator of arr) {
            this.petBagList.push(new PetEntity(iterator));
        }
        this.petWearList = userdata.petWearList != null ? userdata.petWearList : [-1, -1, -1, -1, -1];

        arr = userdata.shopList != null ? userdata.shopList : [];
        for (const iterator of arr) {
            this.shopList.push(new ShopEntity(iterator));
        }

        this.shopResetTime = userdata.shopResetTime != null ? userdata.shopResetTime : getZeroTime();
        this.currentMapId = userdata.currentMapId != null ? userdata.currentMapId : 1;

        var taskArr = userdata.taskList != null ? userdata.taskList : [];
        for (const iterator of taskArr) {
            var taskEy = new TaskEntity();
            taskEy.init(iterator);
            this.taskList.push(taskEy);
        }

        this.signinTime = userdata.signinTime != null ? userdata.signinTime : getZeroTime();
        this.isSignin = userdata.isSignin != null ? userdata.isSignin : false;
        this.signinDay = userdata.signinDay != null ? userdata.signinDay : 0;
        if (userdata.signinArr != null) {
            for (let i = 0; i < 7; i++) {
                var signinEy = new SigninEntity();
                signinEy.init(userdata.signinArr[i]);
                this.signinArr[i] = signinEy;
            }
        } else {
            for (let i = 0; i < 7; i++) {
                var signinEy = new SigninEntity();
                signinEy.day = i + 1;
                signinEy.isReceive = false;
                this.signinArr[i] = signinEy;
            }
        }

        if (userdata.monthCard != null) {
            this.monthCard = new CardEntity();
            this.monthCard.init(userdata.monthCard);
        }
        if (userdata.perpetualCard != null) {
            this.perpetualCard = new CardEntity();
            this.perpetualCard.init(userdata.perpetualCard);
        }


        this.giftArr = userdata.giftArr != null ? userdata.giftArr : [false, false, false, false];


    }

    getShopEntity(id: number): ShopEntity {
        for (const iterator of this.shopList) {
            if (iterator.id == id) {
                return iterator;
            }
        }
        return null;
    }

    getCharacterEntity(id: number): CharacterEntity {
        for (const iterator of this.heroList) {
            if (id == iterator.id) {
                return iterator;
            }
        }
        return null;
    }
    /**
     * 是否是首次通关
     * @param id 关卡id
     * @returns 
     */
    isFirstPassGate(id: number): boolean {
        return this.gatePassList.indexOf(id) == -1;
    }

    isUnlockSkill(id: number): boolean {
        for (const iterator of this.skillList) {
            if (iterator.id == id) {
                return true;
            }
        }

        return false;
    }

    getSkillEntity(id: number): SkillEntity {
        for (const iterator of this.skillList) {
            if (iterator.id == id) {
                return iterator;
            }
        }
        return null;
    }

    getEquipEntity(id: number): EquipEntity {
        for (const iterator of this.equipBagList) {
            if (iterator.id == id) {
                return iterator;
            }
        }
        return null;
    }

    getPetEntity(id: number): PetEntity {
        for (const iterator of this.petBagList) {
            if (iterator.id == id) {
                return iterator;
            }
        }
        return null;
    }

    getTaskEntity(taskType: number): TaskEntity {
        for (const iterator of this.taskList) {
            if (iterator.taskId == taskType) {
                return iterator;
            }
        }

        return null;
    }


}